package leecode;

import java.util.concurrent.locks.ReentrantLock;
import java.util.function.IntConsumer;

public class _1195 {
}


class FizzBuzz {
    private int n;
    private int b = 1;
    private ReentrantLock lock = new ReentrantLock();

    public FizzBuzz(int n) {
        this.n = n;
    }

    public void fizz(Runnable printFizz) throws InterruptedException {
        while (true) {
            lock.lock();
            try {
                int i = b;
                if (i > n) return;
                if (i % 3 == 0 && i % 5 != 0) {
                    printFizz.run();
                    b++;
                }
            } finally {
                lock.unlock();
            }
        }
    }

    public void buzz(Runnable printBuzz) throws InterruptedException {
        while (true) {
            lock.lock();
            try {
                int i = b;
                if (i > n) return;
                if (i % 3 != 0 && i % 5 == 0) {
                    printBuzz.run();
                    b++;
                }
            } finally {
                lock.unlock();
            }
        }
    }

    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        while (true) {
            lock.lock();
            try {
                int i = b;
                if (i > n) return;
                if (i % 3 == 0 && i % 5 == 0) {
                    printFizzBuzz.run();
                    b++;
                }
            } finally {
                lock.unlock();
            }
        }
    }

    public void number(IntConsumer printNumber) throws InterruptedException {
        while (true) {
            lock.lock();
            try {
                int i = b;
                if (i > n) return;
                if (i % 3 != 0 && i % 5 != 0) {
                    printNumber.accept(i);
                    b++;
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
